package com.woniuxy.controller;

import com.woniuxy.pojo.Dept;
import com.woniuxy.service.DeptService;
import com.woniuxy.util.ResultDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/aa")
public class DeptController {
    @Autowired
    private DeptService deptService;
    @GetMapping(value = "/depts")
    public ResultDto selectAllDepts(){
        System.out.println("*************selectAllDepts*********************");
        ResultDto dto=new ResultDto();
        List<Dept> all=deptService.findAllDepts();
        if(all!=null&&all.size()>0){
            dto.setCode(200);
            dto.setMsg("查询数据成功");
            dto.setData(all);
        }else{
            dto.setCode(10001);
            dto.setMsg("查询数据失败");
        }
        return dto;
    }
    @GetMapping(value = "/depts/{id}")
    public ResultDto selectDeptById(@PathVariable int id){

        ResultDto dto=new ResultDto();
        Dept dept=this.deptService.findDeptById(id);
        if(dept!=null){
            dto.setCode(200);
            dto.setMsg("按Id查询部门成功");
            dto.setData(dept);
        }else{
            dto.setCode(10001);
            dto.setMsg("按Id查询部门失败");
        }
        return dto;
    }
    @PostMapping(value = "/depts")
    public ResultDto addDept(@RequestBody Dept dept){
        ResultDto dto=new ResultDto();
        try {
            deptService.addDept(dept);
            dto.setCode(200);
            dto.setMsg("增加部门成功");
        } catch (Exception e) {
            dto.setCode(10001);
            dto.setMsg("增加部门失败");
        }
        return dto;
    }
    @DeleteMapping(value = "/depts/{id}")
    public ResultDto deleteDept(@PathVariable int id){
        ResultDto dto=new ResultDto();
        try {
            this.deptService.deleteDept(id);
            dto.setCode(200);
            dto.setMsg("删除部门成功");
        } catch (Exception e) {
            dto.setCode(10001);
            dto.setMsg("删除部门失败");
        }
        return dto;
    }
    @PutMapping(value = "/depts")
    public ResultDto updateDept(Dept dept){
        ResultDto dto=new ResultDto();
        try {
            this.deptService.updateDept(dept);
            dto.setCode(200);
            dto.setMsg("修改部门成功");
        } catch (Exception e) {
            dto.setCode(10001);
            dto.setMsg("修改部门失败");
        }
        return dto;
    }
}
